
global data ../data
global china ../china/data
global suppdata ../china/suppdata

forv x=2002/2008 {
 use $data/imp`x', clear
 gen cntry=substr(manuf_id,1,2)
 drop if cntry!="CN"
 
 drop if firmid==""
 drop if manuf_id==""

 drop if (qty1_con=="1") | (qty1_con=="2") | (qty1_con=="3")
 drop if (qty1_imp=="Q") | (qty1_imp=="S") | (qty1_imp=="V") | (qty1_imp=="W")

 gen rel=(related=="Y" | related=="R")

 gen month=substr(doi,1,2)
 destring month, replace

*This code is to calculate price using only those observations where qty_1!=0
 gen zero_indicator = 0
 replace zero_indicator = 1 if qty1==0

 collapse (sum) qty1 value duty charges swt (min) month, by(firmid manuf_id rel hs zero_indicator)
 rename month firstmonth
 
 rename qty1 q
 gen v = value*(1-zero_indicator)

 collapse (sum) q v value duty charges swt (min) firstmonth, by(firmid manuf_id rel hs)
 rename value v_zero
 
 gen year=`x'
 
 save $china/imp`x'china, replace
 }
 

** Also, generate an identifier for whether a US firm is a wholesaler or retailer (NAICS 31,32, or 33)
forv x=2002/2006 {
! gunzip $suppdata/wr`x'.dta.gz
}
use $suppdata/wr2002, clear
forv x=2003/2006 {
append using $suppdata/wr`x'
}
duplicates drop firmid, force
save $china/wr, replace
